package com.isoft.controller.flight;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoft.bean.ResponseData;
import com.isoft.controller.BaseController;
import com.isoft.entity.flight.AirportInfo;
import com.isoft.entity.flight.FlightView;
import com.isoft.service.flight.AirportInfoService;
import com.isoft.service.flight.impl.AirportInfoServiceImpl;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 机场信息控制层
 */
@RestController
@RequestMapping("/airport")
@CrossOrigin
public class AirportInfoController extends BaseController {
    @Autowired
    AirportInfoServiceImpl airportInfoService;
    @GetMapping("/getAll")
    public ResponseData<Object> getAllAirportInfo() {
        return returnObject(airportInfoService.getAllAirportInfo());
    }
//    @GetMapping("/get/{META_ID}")
//    public ResponseData<Object> getAirportInfoByMetaId(@PathVariable("META_ID") Integer META_ID,
//                                                       @PathVariable("CNNM") String CNNM,
//                                                       @PathVariable("ENNM") String ENNM) {
//        return returnObject(airportInfoService.getAirportInfoBy(META_ID, CNNM, ENNM));
//    }
    @GetMapping("/get")

    public ResponseData<List<AirportInfo>> getAirportInfoBy(
            @RequestParam(value = "META_ID", required = false) Integer META_ID,
            @RequestParam(value = "CNNM", required = false) String CNNM,
            @RequestParam(value = "ENNM", required = false) String ENNM,
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "7") int pageSize) {
        //使用mybatis-plus的分页查询
        Page<AirportInfo> page=new Page<>(currentPage, pageSize);
        QueryWrapper<AirportInfo> queryWrapper = new QueryWrapper<>();
        //倒序
        queryWrapper.orderByDesc("ID");
        if (META_ID != null &&  META_ID!=0) {
            queryWrapper.like("META_ID", META_ID);
        }
        if (CNNM != null && !CNNM.isEmpty()) {
            queryWrapper.like("CNNM", CNNM);
        }
        if (ENNM != null && !ENNM.isEmpty()) {
            queryWrapper.like("ENNM", ENNM);
        }
        airportInfoService.page(page, queryWrapper);
        return ResponseData.success(page.getRecords(), page.getTotal());
    }
    @PostMapping("/add")
    public ResponseData<Boolean> addAirportInfo(@RequestBody AirportInfo airportInfo) {
        Boolean result = airportInfoService.addAirportInfo(airportInfo);
        if(result){
            return returnBoolean(result, "200", "添加成功");
        }else {
            return returnBoolean(result, "500", "添加失败！该航班号信息已存在");
        }}
    @DeleteMapping("/delete/{id}")
    public ResponseData<Boolean> deleteAirportInfo(@PathVariable("id") Integer ID) {
        Boolean result = airportInfoService.deleteAirportInfo(ID);
        if(result){
            return returnBoolean(result, "200", "删除成功");
        }else {
            return returnBoolean(result, "500", "删除失败！该航班号信息不存在");
        }
    }
    @PutMapping("/update")
    public ResponseData<Boolean> updateAirportInfo(@RequestBody AirportInfo airportInfo) {
        Boolean result = airportInfoService.updateAirportInfo(airportInfo);
        if(result){
            return returnBoolean(result, "200", "修改成功");
        }else {
            return returnBoolean(result, "500", "修改失败！该航班号信息不存在");
        }
    }

    @GetMapping("/page")
    public ResponseData<List<AirportInfo>> getAirportInfoPage(
            @RequestParam(defaultValue = "1") int currentPage,
            @RequestParam(defaultValue = "5") int pageSize) {
        try {
            // 调用服务层获取分页数据
            IPage<AirportInfo> AirportInfoPage = airportInfoService.getAirportInfoPage(currentPage, pageSize);

            // 从IPage中提取数据列表和总记录数
            List<AirportInfo> dataList = AirportInfoPage.getRecords(); // 获取数据列表
            long total = AirportInfoPage.getTotal(); // 获取总记录数
            // 封装到ResponseData中
            return ResponseData.success(dataList, total);
        } catch (Exception e) {
            // 异常处理，返回错误信息
            return ResponseData.failure("500", "获取FlightView信息失败: " + e.getMessage());
        }
    }
}
